/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.util.ArrayUtil;
/*     */ import com.zimbra.common.util.ListUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.StringReader;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.methods.PostMethod;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CheckSpelling
/*     */   extends MailDocumentHandler
/*     */ {
/*     */   private Log log;
/*     */   
/*     */   public CheckSpelling()
/*     */   {
/*  50 */     this.log = ZimbraLog.misc;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  60 */     ZimbraSoapContext zc = getZimbraSoapContext(context);
/*  61 */     Element response = zc.createElement(MailConstants.CHECK_SPELLING_RESPONSE);
/*     */     
/*     */ 
/*  64 */     Provisioning prov = Provisioning.getInstance();
/*  65 */     Server localServer = prov.getLocalServer();
/*  66 */     String[] urls = localServer.getMultiAttr("zimbraSpellCheckURL");
/*  67 */     if (ArrayUtil.isEmpty(urls)) {
/*  68 */       this.log.warn("Unable to check spelling.  No value specified for %s", new Object[] { "zimbraSpellCheckURL" });
/*  69 */       return unavailable(response);
/*     */     }
/*     */     
/*     */ 
/*  73 */     String text = request.getTextTrim();
/*  74 */     if (StringUtil.isNullOrEmpty(text)) {
/*  75 */       this.log.debug("<CheckSpellingRequest> was empty");
/*  76 */       response.addAttribute("available", true);
/*  77 */       return response;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*  82 */     String dictionary = request.getAttribute("dictionary", null);
/*  83 */     Account account = getRequestedAccount(zc);
/*  84 */     if (dictionary == null) {
/*  85 */       dictionary = account.getPrefSpellDictionary();
/*  86 */       if (dictionary == null) {
/*  87 */         dictionary = account.getLocale().toString();
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*  92 */     List<String> ignoreWords = new ArrayList();
/*  93 */     addToList(ignoreWords, account.getPrefSpellIgnoreWord());
/*  94 */     addToList(ignoreWords, prov.getDomain(account).getPrefSpellIgnoreWord());
/*  95 */     addToList(ignoreWords, prov.getCOS(account).getPrefSpellIgnoreWord());
/*     */     
/*  97 */     String ignore = request.getAttribute("ignore", null);
/*  98 */     if (ignore != null) {
/*  99 */       addToList(ignoreWords, ignore.split("[\\s,]+"));
/*     */     }
/*     */     
/* 102 */     String ignorePattern = account.getPrefSpellIgnorePattern();
/*     */     
/*     */ 
/* 105 */     ServerResponse spellResponse = null;
/* 106 */     for (int i = 0; i < urls.length; i++) {
/* 107 */       String url = urls[i];
/*     */       try {
/* 109 */         boolean ignoreAllCaps = account.isPrefSpellIgnoreAllCaps();
/* 110 */         this.log.debug("Checking spelling: url=%s, dictionary=%s, text=%s, ignore=%s, ignoreAllCaps=%b", new Object[] { url, dictionary, text, ignoreWords, Boolean.valueOf(ignoreAllCaps) });
/*     */         
/* 112 */         spellResponse = checkSpelling(url, dictionary, ignoreWords, text, ignoreAllCaps);
/* 113 */         if (spellResponse.statusCode == 200) {
/*     */           break;
/*     */         }
/*     */       } catch (IOException ex) {
/* 117 */         ZimbraLog.mailbox.warn("An error occurred while contacting " + url, ex);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 122 */     if (spellResponse == null) {
/* 123 */       return unavailable(response);
/*     */     }
/* 125 */     if (spellResponse.statusCode != 200) {
/* 126 */       throw ServiceException.FAILURE("Spell check failed: " + spellResponse.content, null);
/*     */     }
/*     */     
/*     */ 
/* 130 */     if (spellResponse.content != null) {
/* 131 */       BufferedReader reader = new BufferedReader(new StringReader(spellResponse.content));
/*     */       
/* 133 */       String line = null;
/* 134 */       int numLines = 0;
/* 135 */       int numMisspelled = 0;
/*     */       try {
/* 137 */         while ((line = reader.readLine()) != null)
/*     */         {
/* 139 */           line = line.trim();
/* 140 */           numLines++;
/* 141 */           int colonPos = line.indexOf(':');
/*     */           
/* 143 */           if (colonPos > 0) {
/* 144 */             String word = line.substring(0, colonPos);
/* 145 */             String suggestions = line.substring(colonPos + 1, line.length());
/* 146 */             if ((ignorePattern == null) || (!word.matches(ignorePattern)))
/*     */             {
/*     */ 
/* 149 */               Element wordEl = response.addElement("misspelled");
/* 150 */               wordEl.addAttribute("word", word);
/* 151 */               wordEl.addAttribute("suggestions", suggestions);
/* 152 */               numMisspelled++;
/*     */             }
/*     */           }
/*     */         }
/* 156 */       } catch (IOException e) { this.log.warn("IOException checking spelling", e);
/* 157 */         return unavailable(response);
/*     */       }
/* 159 */       this.log.debug("CheckSpelling: found %d misspelled words in %d lines", new Object[] { Integer.valueOf(numMisspelled), Integer.valueOf(numLines) });
/*     */     }
/*     */     
/*     */ 
/* 163 */     response.addAttribute("available", true);
/* 164 */     return response;
/*     */   }
/*     */   
/*     */   private void addToList(List<String> list, String[] elements) {
/* 168 */     if (elements == null) {
/* 169 */       return;
/*     */     }
/* 171 */     for (String element : elements) {
/* 172 */       if (!StringUtil.isNullOrEmpty(element)) {
/* 173 */         list.add(element);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private ServerResponse checkSpelling(String url, String dictionary, List<String> ignoreWords, String text, boolean ignoreAllCaps) throws IOException
/*     */   {
/* 180 */     PostMethod post = new PostMethod(url);
/* 181 */     post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
/* 182 */     if (dictionary != null) {
/* 183 */       post.addParameter("dictionary", dictionary);
/*     */     }
/* 185 */     if (text != null) {
/* 186 */       post.addParameter("text", text);
/*     */     }
/* 188 */     if (!ListUtil.isEmpty(ignoreWords)) {
/* 189 */       post.addParameter("ignore", StringUtil.join(",", ignoreWords));
/*     */     }
/* 191 */     if (ignoreAllCaps) {
/* 192 */       post.addParameter("ignoreAllCaps", "on");
/*     */     }
/* 194 */     HttpClient http = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient();
/* 195 */     ServerResponse response = new ServerResponse(null);
/*     */     try {
/* 197 */       response.statusCode = HttpClientUtil.executeMethod(http, post);
/* 198 */       response.content = post.getResponseBodyAsString();
/*     */     } finally {
/* 200 */       post.releaseConnection();
/*     */     }
/* 202 */     return response;
/*     */   }
/*     */   
/*     */   private Element unavailable(Element response) {
/* 206 */     response.addAttribute("available", false);
/* 207 */     return response;
/*     */   }
/*     */   
/*     */   private class ServerResponse
/*     */   {
/*     */     int statusCode;
/*     */     String content;
/*     */     
/*     */     private ServerResponse() {}
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/CheckSpelling.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */